﻿using WeDonekRpc.Helper;
using WeDonekRpc.Helper.Validate;
namespace Shop.RemoteModel.Coupon.Model
{
    public class CouponSet
    {
        /// <summary>
        /// 优惠卷标题
        /// </summary>
        [NullValidate("shop.coupon.title.null")]
        [LenValidate("shop.coupon.title.len", 2, 50)]
        public string CouponTitle { get; set; }

        /// <summary>
        /// 优惠卷类型 
        ///</summary>
        [EnumValidate("shop.coupon.type.error", typeof(CouponType))]
        public CouponType CouponType { get; set; }

        /// <summary>
        /// 优惠信息
        /// </summary>
        [NullValidate("shop.coupon.discount.null")]
        [LenValidate("shop.coupon.discount.len", 1, 3)]
        public CouponDiscount[] Discount { get; set; }

        /// <summary>
        /// 最大优惠值 
        ///</summary>
        [EntrustValidate("_Check")]
        [NumValidate("shop.coupon.max.val.error", 0.1)]
        public decimal? MaxCouponVal { get; set; }

        /// <summary>
        /// 投放量
        /// </summary>
        [NumValidate("shop.coupon.putIn.num.error", 1, 1000000)]
        public int PutInNum { get; set; }

        /// <summary>
        /// 投放开始时间 
        ///</summary>
        [TimeValidate("shop.coupon.putIn.begin.error", TimeFormat.分, 0)]
        public DateTime PutInBegin { get; set; }

        /// <summary>
        ///  投放结束时间 
        ///</summary>
        [TimeValidate("shop.coupon.putIn.end.error", TimeFormat.分, 1)]
        [TimeComparisonValidate("shop.coupon.putIn.end.range", "PutInBegin", NumComparisonType.大于)]
        [TimeComparisonValidate("shop.coupon.putIn.use.range", "UseEnd", NumComparisonType.小于等于)]
        public DateTime PutInEnd { get; set; }

        /// <summary>
        /// 开始时间
        /// </summary>
        [TimeValidate("shop.coupon.use.begin.error", TimeFormat.分, 0)]
        public DateTime? UseBegin
        {
            get;
            set;
        }
        /// <summary>
        /// 结束时间
        /// </summary>
        [TimeValidate("shop.coupon.use.end.error", TimeFormat.分, 1)]
        [TimeComparisonValidate("shop.coupon.use.end.range", "UseBegin", NumComparisonType.大于)]
        public DateTime? UseEnd
        {
            get;
            set;
        }
        /// <summary>
        /// 领取后过期天数
        /// </summary>
        [NumValidate("shop.coupon.expire.day.error", 1)]
        public int? ExpireDay
        {
            get;
            set;
        }
        /// <summary>
        /// 用户范围
        /// </summary>
        [EnumValidate("shop.coupon.user.range.error", typeof(CouponUserRange))]
        public CouponUserRange UserRange
        {
            get;
            set;
        }

        /// <summary>
        /// 等级ID
        /// </summary>
        [NumValidate("shop.user.grade.id.error", 1)]
        public long? GradeId { get; set; }
        /// <summary>
        /// 投放商品范围
        /// </summary>
        [EnumValidate("shop.coupon.putIn.range.error", typeof(CouponPutInRange))]
        public CouponPutInRange PutInRange { get; set; }

        /// <summary>
        /// 用户领取限制
        /// </summary>
        [EnumValidate("shop.coupon.receive.type.error", typeof(CouponLimitReceiveType))]
        public CouponLimitReceiveType ReceiveType { get; set; }


        /// <summary>
        /// 单次领取数 
        ///</summary>
        [NumValidate("shop.coupon.receive.num.error", 1)]
        public int ReceiveNum { get; set; }

        /// <summary>
        /// 是否公开
        /// </summary>
        public bool IsPublic { get; set; }

        private static void _Check (CouponSet obj)
        {
            if (obj.CouponType == CouponType.满减)
            {
                if (!obj.Discount.TrueForAll(c => c.CouponVal.HasValue))
                {
                    throw new ErrorException("shop.coupon.val.null");
                }
            }
            else if (!obj.Discount.TrueForAll(c => c.Discount.HasValue))
            {
                throw new ErrorException("shop.coupon.discount.val.null");
            }
            else if (obj.MaxCouponVal.HasValue == false)
            {
                throw new ErrorException("shop.coupon.max.val.null");
            }
            if (( !obj.UseBegin.HasValue || !obj.UseEnd.HasValue ) && !obj.ExpireDay.HasValue)
            {
                throw new ErrorException("shop.coupon.use.time.null");
            }
        }

    }
}
